package com.hy.mydesktop.client.component.factory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.Component;
import com.extjs.gxt.ui.client.widget.Layout;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormButtonBinding;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.FormPanel.Method;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.hy.mydesktop.client.component.meta.FormModuleModelEnum;
import com.hy.mydesktop.client.component.type.ComponentTypeEnum;
import com.hy.mydesktop.shared.persistence.domain.gxt.GxtComponentMetaNodeModel;


/**
 * 
 * <ul>
 * <li>开发作者：花宏宇</li>
 * <li>设计日期：2010-9-21；时间：下午03:05:22</li>
 * <li>类型名称：FormModuleFactory</li>
 * <li>设计目的：创建表单组件</li>
 * </ul>
 * <ul>
 * <b>修订编号：</b>
 * <li>修订日期：</li>
 * <li>修订作者：</li>
 * <li>修订原因：</li>
 * <li>修订内容：</li>
 * </ul>
 */
public class FormModuleFactory {

	public static FormPanel createFormModule(
			GxtComponentMetaNodeModel gxtComponentMetaNodeModel) {
		return configureFormPanel(gxtComponentMetaNodeModel);
/**
 * 
 */
	}

	
	private static FormPanel configureFormPanel(
			GxtComponentMetaNodeModel gxtComponentMetaNodeModel) {

		 FormPanel formpanel = createDefaultFormPanel();
		 
		 formpanel = AbstractComponetFactory.configureAbstractComponet(formpanel, gxtComponentMetaNodeModel);
		
		 
		 //读取出formitemlist中的每一项，并创建，然后放入到formPanel中
		 if (gxtComponentMetaNodeModel.get(FormModuleModelEnum.FORMITEMLIST.name()
					.toLowerCase()) != null) {
			 List<GxtComponentMetaNodeModel> list = (List<GxtComponentMetaNodeModel>) gxtComponentMetaNodeModel.get(FormModuleModelEnum.FORMITEMLIST.name().toLowerCase());
			 for (GxtComponentMetaNodeModel subGxtComponentMetaNodeModel : list) {
					formpanel.add(createBaseGxtComponent(subGxtComponentMetaNodeModel));
			}

		}
		if (gxtComponentMetaNodeModel.get(FormModuleModelEnum.HIGHT.name()
				.toLowerCase()) != null) {
			Integer height = (Integer) gxtComponentMetaNodeModel
					.get(FormModuleModelEnum.HIGHT.name().toLowerCase());
			formpanel.setHeight((int) height);

		}
		if (gxtComponentMetaNodeModel.get(FormModuleModelEnum.WIDTH.name()
				.toLowerCase()) != null) {
			Integer width = (Integer) gxtComponentMetaNodeModel
					.get(FormModuleModelEnum.WIDTH.name().toLowerCase());
			formpanel.setWidth((int) width);

		}
		if (gxtComponentMetaNodeModel.get(FormModuleModelEnum.ACTION.name()
				.toLowerCase()) != null) {
			String url = (String) gxtComponentMetaNodeModel
					.get(FormModuleModelEnum.ACTION.name().toLowerCase());
			formpanel.setAction(url);

		} 
		if (gxtComponentMetaNodeModel.get(FormModuleModelEnum.BUTTONALIGN
				.name().toLowerCase()) != null) {
			String buttonAlin = ((String) gxtComponentMetaNodeModel
					.get(FormModuleModelEnum.BUTTONALIGN.name().toLowerCase()))
					.toUpperCase();
			if (buttonAlin.equals("CENTER")) {
				formpanel.setButtonAlign(HorizontalAlignment.CENTER);
			}else if (buttonAlin.equals("LEFT")) {
				formpanel.setButtonAlign(HorizontalAlignment.LEFT);
			}
		}
		if (gxtComponentMetaNodeModel.get(FormModuleModelEnum.COLLAPSIBLE
				.name().toLowerCase()) != null) {
			Boolean collapsible = (Boolean) gxtComponentMetaNodeModel
					.get(FormModuleModelEnum.COLLAPSIBLE.name().toLowerCase());
			formpanel.setCollapsible((boolean) collapsible);

		} 
		if (gxtComponentMetaNodeModel.get(FormModuleModelEnum.FRAME.name()
				.toLowerCase()) != null) {
			Boolean frame = (Boolean) gxtComponentMetaNodeModel
					.get(FormModuleModelEnum.FRAME.name().toLowerCase());
			formpanel.setFrame((boolean) frame);

		}
		if (gxtComponentMetaNodeModel.get(FormModuleModelEnum.HEADERVISIBLE
				.name().toLowerCase()) != null) {
			Boolean headerVisible = (Boolean) gxtComponentMetaNodeModel
					.get(FormModuleModelEnum.HEADERVISIBLE.name().toLowerCase());
			formpanel.setHeaderVisible(headerVisible);

		}
		if (gxtComponentMetaNodeModel.get(FormModuleModelEnum.HEADING.name()
				.toLowerCase()) != null) {
			String text = (String) gxtComponentMetaNodeModel
					.get(FormModuleModelEnum.HEADING.name().toLowerCase());
			formpanel.setHeading(text);

		} 
		if (gxtComponentMetaNodeModel.get(FormModuleModelEnum.LAYOUT.name()
				.toLowerCase()) != null) {
			ComponentTypeEnum componentTypeEnum = (ComponentTypeEnum) gxtComponentMetaNodeModel.get(FormModuleModelEnum.LAYOUT.name()
					.toLowerCase());
	
			Layout layout=null;
	
			switch (componentTypeEnum) {
	            case FITLAYOUT_MODEL:
	            		layout=FitLayoutFactory.createFitLayout(gxtComponentMetaNodeModel);
	            		formpanel.setLayout(layout);
	                break;
	            case ACCORDIONLAYOUT_MODEL:
	                	layout=AccordionLayoutFactory.createAccordionLayout(gxtComponentMetaNodeModel);
	                	formpanel.setLayout(layout);
	                break;
	            case ROWLAYOUT_MODEL:
	                	layout=RowLayoutFactory.createRowLayout(gxtComponentMetaNodeModel);
	                	formpanel.setLayout(layout);
	                break;
	            case VBOXLAYOUT_MODEL:
	                	layout=VboxLayoutFactory.createVboxLayout(gxtComponentMetaNodeModel);
	                	formpanel.setLayout(layout);
	                break;
	            case COLUMNLAYOUT_MODEL:
	                	layout=ColumnLayoutFactory.createColumnLayout(gxtComponentMetaNodeModel);
	                	formpanel.setLayout(layout);
	                break;
	            case FLOWLAYOUT_MODEL:
	                	layout=FlowLayoutFactory.createFlowLayout(gxtComponentMetaNodeModel);
	                	formpanel.setLayout(layout);
	                break;
	            case TABLELAYOUT_MODEL:
	                	layout=TableLayoutFactory.createTableLayout(gxtComponentMetaNodeModel);
	                	formpanel.setLayout(layout);
	                break;
	            case FORMLAYOUT_MODEL:
	                	layout=FormLayoutFactory.createFormLayout(gxtComponentMetaNodeModel);
	                	
	                	formpanel.setLayout(layout);
	                break;
	            default:
	                break;
	        }

		} 
		if (gxtComponentMetaNodeModel.get(FormModuleModelEnum.METHOD.name()
				.toLowerCase()) != null) {
			String method = ((String) gxtComponentMetaNodeModel
					.get(FormModuleModelEnum.HEADING.name().toLowerCase()))
					.toUpperCase();
			if(method.equals("POST")){
				formpanel.setMethod(Method.POST);
			}else{
				formpanel.setMethod(Method.GET);
			}

		} 
		if (gxtComponentMetaNodeModel.get(FormModuleModelEnum.ICON.name()
				.toLowerCase()) != null) {
			AbstractImagePrototype icon = (AbstractImagePrototype) gxtComponentMetaNodeModel
					.get(FormModuleModelEnum.ICON.name().toLowerCase());
			formpanel.setIcon(icon);

		}
		if (gxtComponentMetaNodeModel.get(FormModuleModelEnum.AUTOWIDTH.name()
				.toLowerCase()) != null) {
			boolean autoWidth=(Boolean)gxtComponentMetaNodeModel.get(FormModuleModelEnum.AUTOWIDTH.name()
					.toLowerCase());
			formpanel.setAutoWidth(autoWidth);
		}
		if (gxtComponentMetaNodeModel.get(FormModuleModelEnum.AUTOHEIGHT.name()
				.toLowerCase()) != null) {
			boolean autoHeight=(Boolean)gxtComponentMetaNodeModel.get(FormModuleModelEnum.AUTOHEIGHT.name()
					.toLowerCase());
			formpanel.setAutoHeight(autoHeight);

		}
		if(gxtComponentMetaNodeModel.get(FormModuleModelEnum.ADDBUTTON.name()
				.toLowerCase()) != null){
			List<GxtComponentMetaNodeModel> list=(List<GxtComponentMetaNodeModel>)gxtComponentMetaNodeModel.get(FormModuleModelEnum.ADDBUTTON.name()
					.toLowerCase());
			for(int i=0;i<list.size();i++)
			{
				Button button=ButtonFactory.createButton(list.get(i));
				if(i==0)
				{
					FormButtonBinding binding=new FormButtonBinding(formpanel);
					 binding.addButton(button);
				}else
				{
					button.addSelectionListener(new SelectionListener<ButtonEvent>() {
						
						@Override
						public void componentSelected(ButtonEvent ce) {
							//换成MessageBox
							Map<String, Object> box=new HashMap<String, Object>();
							box.put("component_model_type", ComponentTypeEnum.MESSAGEBOX_MODEL);
							box.put("type", "confirm");
							box.put("title", "温馨提示");
							box.put("message", "您确定要取消吗？");
							GxtComponentMetaNodeModel box1=new GxtComponentMetaNodeModel(box);
							MessageBoxFactory.createMessageBox(box1);
						}
					});
				}
				formpanel.addButton(button);
			}			
		}
		return formpanel;
	}

	private static FormPanel createDefaultFormPanel() {

		FormPanel formpanel = new FormPanel();
		return formpanel;
	}
	
	private static <COMPONENT extends Component> COMPONENT createBaseGxtComponent(
			GxtComponentMetaNodeModel gxtComponentMetaNodeModel) {
		//BaseModel gxtComponentMetaModel=null;
		
		COMPONENT component = null;
				
		
		ComponentTypeEnum componentTypeEnum = gxtComponentMetaNodeModel.getComponentTypeEnum();
		
		switch (componentTypeEnum) {
		//逐一枚举出ComponentTypeEnum中所有的控件类型
			
			case FORM_MODULE_MODEL: {
				component = (COMPONENT) FormModuleFactory.createFormModule(gxtComponentMetaNodeModel);
				break;
			}
			case BUTTON_MODEL: {
				component = (COMPONENT) ButtonFactory.createButton(gxtComponentMetaNodeModel);
				break;
			}
			case EDITABLEGRID_MODEL: {
				component = (COMPONENT) EditableGridFactory.createEditorGrid(gxtComponentMetaNodeModel);
				break;
			}
			
			case WINDOW_MODEL: {
				component = (COMPONENT) WindowFactory.createWindow(gxtComponentMetaNodeModel);
				break;
			}
			
			case TEXTFIELD_MODEL: {
				component = (COMPONENT) TextFieldFactory.createTextField(gxtComponentMetaNodeModel);
				break;
			}
			case CONTENTPANEL_MODEL: {
				component = (COMPONENT) ContentPanelFactory.createContentPanel(gxtComponentMetaNodeModel);
				break;
			}
			case MENUBAR_MODEL: {
				component = (COMPONENT)MenuBarFactory.createMenuBar(gxtComponentMetaNodeModel);
				break;
			}
			case TOOLBAR_MODEL: {
				component = (COMPONENT)ToolBarFactory.createToolBar(gxtComponentMetaNodeModel);
				break;
			}
			case FIELDSET_MODEL: {
				component = (COMPONENT)FieldSetFactory.createFieldSet(gxtComponentMetaNodeModel);
				break;
			}
			case DATEFIELD_MODEL: {
				component = (COMPONENT)DateFieldFactory.createDateField(gxtComponentMetaNodeModel);
				break;
			}
			case DATEPICKER_MODEL: {
				component = (COMPONENT)DatePickerFactory.createDatePicker(gxtComponentMetaNodeModel);
				break;
			}
			case COMBOBOX_MODEL: {
				component = (COMPONENT)ComboBoxFactory.createComboBox(gxtComponentMetaNodeModel);
				break;
			}
			case MENUBARITEM_MODEL: {
				component = (COMPONENT)MenuItemBarFactory.createMenuBarItem(gxtComponentMetaNodeModel);
				break;
			}
			case FORMPANEL_MODEL: {
				component = (COMPONENT)FormpanelFactory.createFormPanel(gxtComponentMetaNodeModel);
				break;
			}
			case TEXTAREA_MODEL: {
				component = (COMPONENT)TextAreaFactory.createTextArea(gxtComponentMetaNodeModel);
				break;
			}
			case CHECKBOX_MODEL: {
				component = (COMPONENT)CheckBoxFactory.createCheckBox(gxtComponentMetaNodeModel);
				break;
			}
			case CHART_MODEL: {
				//component = (COMPONENT)ChartFactory.createChart(gxtComponentMetaNodeModel);
				break;
			}
			default: {//当componentTypeEnum为空，或者与现有控件类型不符合时
				component = null;
				break;
			}
			}
			return component;
	}
	
}
